import Ajax from 'axios';
import * as Upload from './upload';
import Headers from './headers';
import Response from './response';
import ResponseError from './response/error';

const Instance = Ajax.create({
    timeout: 30 * 1000
})

const {
    request,
    response
} = Instance.interceptors;

request.use(Headers);
response.use(Response, ResponseError);

class service {
    static post = (
        url: string, data?: any
    ): Promise<any> => {
        return Instance.post(url, data);
    }

    static put = (
        url: string, data?: any
    ): Promise<any> => {
        return Instance.put(url, data);
    }

    static get = (
        url: string, data?: any
    ): Promise<any> => {
        const _config = {
            params: data
        }
        return Instance.get(url, _config)
    }

    static delete = (
        url: string, data?: any
    ): Promise<any> => {
        const _config = {
            params: data
        }
        return Instance.delete(url, _config)
    }

    static uploadImage = Upload.image;
    static uploadVideo = Upload.video;
}

export default service;
export const instance = Instance;
